package com.schooldate.util.push;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by etund on 2015/5/3 0003.
 */
public class PushUtils {

    /**
     * 推送给单个用户
     * @throws Exception
     */
    public static void pushToSingle(String devicetoken,String content) throws Exception{
        IGtPush push = new IGtPush(APPKEY, MASTERSECRET);
        TransmissionTemplate template = TransmissionTemplateDemo();

        template.setPushInfo("actionLocKey", 1, "message", "sound", "payload",
                content, "locArgs", "launchImage");
        SingleMessage message = new SingleMessage();
        message.setData(template);
        IPushResult ret0 = push.pushAPNMessageToSingle(APPID, devicetoken, message);
        System.out.println(ret0.getResponse());
    }



    /**
     * 推送给一些用户
     * @throws Exception
     */
    public static void pushToList(ArrayList<String> devicetokens,String content) throws Exception{
        IGtPush push = new IGtPush(APPKEY, MASTERSECRET);
        push.connect();

        TransmissionTemplate template = TransmissionTemplateDemo();
        template.setPushInfo("actionLocKey", 3, "message", "sound", "payload",
                content, "locArgs", "launchImage");
        ListMessage message = new ListMessage();
        message.setData(template);

        String contentId = push.getAPNContentId(APPID, message);
        System.out.println(contentId);
        List<String> dtl = devicetokens;
//        dtl.add(devicetoken);
        System.setProperty("gexin.rp.sdk.pushlist.needDetails", "true");
        IPushResult ret = push.pushAPNMessageToList(APPID, contentId, dtl);
        System.out.println(ret.getResponse());
    }


    /**
     * 推送给所有用户
     * @throws Exception
     */
    public static void pushToApp(String content) throws Exception{
        IGtPush push = new IGtPush( APPKEY, MASTERSECRET);
        push.connect();

        TransmissionTemplate template = TransmissionTemplateDemo();
        template.setPushInfo("actionLocKey", 3, "message", "sound", "payload",
                content, "locArgs", "launchImage");
        AppMessage message = new AppMessage();
        message.setData(template);

        message.setOffline(true);
        message.setOfflineExpireTime(1 * 1000 * 3600);

        List<String> appIdList = new ArrayList<String>();
        List<String> phoneTypeList = new ArrayList<String>();
        List<String> provinceList = new ArrayList<String>();
        List<String> tagList = new ArrayList<String>();

        appIdList.add(APPID);
        phoneTypeList.add(null);
        provinceList.add(null);
        tagList.add(null);

        message.setAppIdList(appIdList);
        message.setPhoneTypeList(phoneTypeList);
//		message.setProvinceList(provinceList);
//		message.setTagList(tagList);
        message.setPushNetWorkType(1);
        IPushResult ret = push.pushMessageToApp(message,null);
        System.out.println(ret.getResponse().toString());
    }


    protected static final String APPID = "s2MQqHc2RR64aujx7W87j7";
    protected static final String APPKEY = "GF5KFPpWqH9ub5BrorfLu2";
    protected static final String MASTERSECRET = "uquVuQHpJf8A90hy8y6NC9";
//    protected static final String CLIENTID = "f8b14fc288a21bc3d675190e9a4db0a4";
//    protected static final String devicetoken =  "42b74bacb28414c856505e2e6d70fe7940540a8c8fa4c728b3338c9ec1575367";
//    protected static final String API = "http://sdk.open.api.igexin.com/apiex.htm";     //OpenService接口地址

    protected static String getDate(){
        Date date = new Date();
        return date.toLocaleString();
    }

    protected static TransmissionTemplate TransmissionTemplateDemo() throws Exception{
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(APPID);
        template.setAppkey(APPKEY);
        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动。
        template.setTransmissionType(1);
        template.setTransmissionContent("测试测试");

    /*iOS 推送需要对该字段进行设置具体参数详见iOS模板说明*/

//        template.setPushInfo(actionKey, badge, message, sound)
    /*template.setPushInfo("actionLocKey", 4, "message", "sound",
    "payload", "locKey", "locArgs", "launchImage","ContentAvailable");*/
        System.out.println(template+"============");
        return template;
    }

}
